﻿using DevComponents.DotNetBar;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI
{
    public partial class FormPath : Office2007Form
    {
        private List<string> _pointNames;
        public PointPath CurrentPointPath { get; set; }
        public FormPath(List<string> names, PointPath path)
        {
            InitializeComponent();
            this.EnableGlass = false;
            this._pointNames = names;
            this.CurrentPointPath = path;
            if(this.CurrentPointPath == null)
                this.CurrentPointPath = new PointPath();
        }

        private void FormPath_Load(object sender, EventArgs e)
        {
            tV_Flow.DragEnter += FlowTree_DragEnter;
            tV_Flow.DragDrop += FlowTree_DragDrop;
            tV_Flow.ItemDrag += FlowTree_ItemDrag;

            TreeNode rootNode = new TreeNode("Path");
            tV_Flow.Nodes.Add(rootNode);
            if(CurrentPointPath != null)
            {
                foreach (var item in CurrentPointPath.Steps)
                {
                    rootNode.Nodes.Add(item.ToTreeNode());
                }
            }
            tV_Flow.ExpandAll();
        }
        private void FlowTree_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(string)) ||
                e.Data.GetDataPresent(typeof(TreeNode)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }
        private void FlowTree_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode DropNode;
            TreeView DropTreeView;
            DropTreeView = ((TreeView)sender);
            Point Position = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
            DropNode = ((TreeView)sender).GetNodeAt(Position);
            if (DropNode == null)
                return;
            //新增
            Step step = null;
            if (e.Data.GetDataPresent(typeof(string)))
            {
                string toolName = e.Data.GetData(typeof(string)).ToString();
                if(toolName.Equals("btn_MultiAxis"))
                {
                    step = new MultiAxisMoveStep("多轴移动", StepType.MultiAxisMove);
                    FormMultiAxisMoveStep multiAxisStepForm = new FormMultiAxisMoveStep(step, _pointNames);
                    multiAxisStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (multiAxisStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                else if(toolName.Equals("btn_SingleAxis"))
                {
                    step = new SingleAxisMoveStep("单轴移动", StepType.SingleAxisMove);
                    FormSingleAxisMoveStep singleAxisStepForm = new FormSingleAxisMoveStep(step, _pointNames);
                    singleAxisStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (singleAxisStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                else if(toolName.Equals("btn_SingleAxisGoHome"))
                {
                    step = new SingleAxisGoHomeStep("单轴回零", StepType.SingleAxisGoHome);
                    FormSingleAxisGoHomeStep singleAxisStepForm = new FormSingleAxisGoHomeStep(step);
                    singleAxisStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (singleAxisStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                else if (toolName.Equals("btn_DigitalIO"))
                {
                    step = new DigitalOutputStep("数字输出", StepType.DigitalOutput);
                    FormDigitalOutputStep digitalOutputStepForm = new FormDigitalOutputStep(step, _pointNames);
                    digitalOutputStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (digitalOutputStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                else if (toolName.Equals("btn_Delay"))
                {
                    step = new DelayStep("延时", StepType.Delay);
                    FormDelayStep delayStepForm = new FormDelayStep(step, _pointNames);
                    delayStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (delayStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                else if (toolName.Equals("btn_Input"))
                {
                    step = new DigitalInputStep("数字输入", StepType.DigitalInput);
                    FormDigitalInputStep inputStepForm = new FormDigitalInputStep(step, _pointNames);
                    inputStepForm.StartPosition = FormStartPosition.CenterParent;
                    if (inputStepForm.ShowDialog() == DialogResult.OK)
                    {
                        addNewStep(DropNode, step);
                    }
                }
                
            }
        }
        private void FlowTree_ItemDrag(object sender, ItemDragEventArgs e)
        {
            this.DoDragDrop(e.Item as TreeNode, DragDropEffects.Copy | DragDropEffects.Move);
        }
        private void addNewStep(TreeNode DropNode, Step step)
        {
            if (DropNode.Text.Equals("Path"))
            {
                CurrentPointPath.Steps.Insert(0, step);
                this.tV_Flow.Nodes[0].Nodes.Insert(0, step.ToTreeNode());
                this.tV_Flow.ExpandAll();
            }
            else
            {
                int index = CurrentPointPath.Steps.IndexOf(DropNode.Tag as Step);
                CurrentPointPath.Steps.Insert(index + 1, step);
                this.tV_Flow.Nodes[0].Nodes.Insert(index + 1, step.ToTreeNode());
                this.tV_Flow.ExpandAll();
            }
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btn_MultiAxis_MouseDown(object sender, MouseEventArgs e)
        {
            ((ButtonX)sender).DoDragDrop(((ButtonX)sender).Name, DragDropEffects.Copy | DragDropEffects.Move);
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Step step = (tV_Flow.SelectedNode).Tag as Step;
            if (step != null)
            {
                if (step is MultiAxisMoveStep)
                {
                    FormMultiAxisMoveStep multiAxisStepForm = new FormMultiAxisMoveStep(step, _pointNames);
                    if (multiAxisStepForm.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = multiAxisStepForm.CurrentStep.ToString();
                    }
                }
                else if(step is SingleAxisMoveStep)
                {
                    FormSingleAxisMoveStep singleAxisStepForm = new FormSingleAxisMoveStep(step, _pointNames);
                    if (singleAxisStepForm.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = singleAxisStepForm.CurrentStep.ToString();
                    }
                }
                else if(step is DigitalOutputStep)
                {
                    FormDigitalOutputStep digitalOutputStepForm = new FormDigitalOutputStep(step, _pointNames);
                    if (digitalOutputStepForm.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = digitalOutputStepForm.CurrentStep.ToString();
                    }
                }
                else if (step is DigitalInputStep)
                {
                    FormDigitalInputStep digitalOutputStepForm = new FormDigitalInputStep(step, _pointNames);
                    if (digitalOutputStepForm.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = digitalOutputStepForm.CurrentStep.ToString();
                    }
                }
                else if(step is DelayStep)
                {
                    FormDelayStep delayStepForm = new FormDelayStep(step, _pointNames);
                    if (delayStepForm.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = delayStepForm.CurrentStep.ToString();
                    }
                }
                else if(step is SingleAxisGoHomeStep)
                {
                    FormSingleAxisGoHomeStep gohomeStep = new FormSingleAxisGoHomeStep(step);
                    if(gohomeStep.ShowDialog() == DialogResult.OK)
                    {
                        currentSelectedNode.Text = gohomeStep.CurrentStep.ToString();
                    }
                }
            }
        }

        TreeNode currentSelectedNode = null;
        private void tV_Flow_MouseDown(object sender, MouseEventArgs e)
        {
            Point ClickPoint = new Point(e.X, e.Y);
            currentSelectedNode = ((TreeView)sender).GetNodeAt(ClickPoint);
            if (currentSelectedNode != null)//判断你点的是不是一个节点
            {
                ((TreeView)sender).SelectedNode = currentSelectedNode;//选中这个节点
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(currentSelectedNode != null)
            {
                if(MessageBox.Show("确定删除该步骤吗?","路径",MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    Step step = currentSelectedNode.Tag as Step;
                    CurrentPointPath.Steps.Remove(step);
                    tV_Flow.Nodes.Remove(currentSelectedNode);
                }
            }
        }

        private void btn_Copy_Click(object sender, EventArgs e)
        {
            GlobalVariable.CopyPath = this.CurrentPointPath;
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (GlobalVariable.CopyPath == null)
                return;
            if(MessageBox.Show("确定要粘贴路径吗?", "粘贴路径", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.CurrentPointPath.Steps.Clear();
                for (int i = 0; i < GlobalVariable.CopyPath.Steps.Count; i++)
                {
                    Step newStep = new Step();
                    newStep = (Step)GlobalVariable.CopyPath.Steps[i].Clone();
                    this.CurrentPointPath.Steps.Add(newStep);
                }
                tV_Flow.Nodes.Clear();

                TreeNode rootNode = new TreeNode("Path");
                tV_Flow.Nodes.Add(rootNode);
                if (CurrentPointPath != null)
                {
                    foreach (var item in CurrentPointPath.Steps)
                    {
                        rootNode.Nodes.Add(item.ToTreeNode());
                    }
                }
                tV_Flow.ExpandAll();

                //if (!SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                //    SingletonProvider<MachineResource>.Instance.ModulePaths.Add(_moduleName, new SerializableDictionary<string, PointPath>());
                //SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].Add(name, pathForm.CurrentPointPath);
                //XmlSerializerHelper<MachineResource>.SerializeToFile(SingletonProvider<MachineResource>.Instance, PathHelper.GetConfigPath("Machine") + "Machine.xml");
            }
        }
    }
}
